package com.cmc6.user.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cmc6.user.mapper.UserLoginWxMapper;
import com.cmc6.user.model.dto.LoginByOtherUserDTO;
import com.cmc6.user.model.dto.UserLoginInsertOrUpdateDTO;
import com.cmc6.user.model.dto.UserLoginWxRegByWxOpenidDTO;
import com.cmc6.user.model.entity.UserLoginWxDO;
import com.cmc6.user.model.entity.UserSecurityDO;
import com.cmc6.user.service.RegByWxService;
import com.cmc6.user.service.UserRegHelperService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserLoginWxServiceImpl extends ServiceImpl<UserLoginWxMapper, UserLoginWxDO>
    implements RegByWxService {

    @Resource
    UserRegHelperService userRegHelperService;

    /**
     * 通过，微信的 openid，进行注册
     */
    @Override
    public LoginByOtherUserDTO regByWxOpenid(UserLoginWxRegByWxOpenidDTO dto) {

        UserLoginInsertOrUpdateDTO userLoginInsertOrUpdateDTO = new UserLoginInsertOrUpdateDTO();
        userLoginInsertOrUpdateDTO.setEmail("");
        userLoginInsertOrUpdateDTO.setPassword(""); // 设置密码为空
        userLoginInsertOrUpdateDTO.setPhone(dto.getPhone());

        UserSecurityDO userSecurityDO = userRegHelperService.regBase(userLoginInsertOrUpdateDTO, false);

        UserLoginWxDO userLoginWxDO = new UserLoginWxDO();
        userLoginWxDO.setUserId(userSecurityDO.getUserId());
        userLoginWxDO.setOpenid(dto.getOpenid());
        baseMapper.insert(userLoginWxDO); // 操作数据库

        LoginByOtherUserDTO userByOpenIdDTO = new LoginByOtherUserDTO();
        userByOpenIdDTO.setUserId(userSecurityDO.getUserId());
        userByOpenIdDTO.setJwtSecretSuf(userSecurityDO.getJwtSecretSuf());
        userByOpenIdDTO.setEnableFlag(true);

        return userByOpenIdDTO;
    }

}




